拓扑排序代码实现


package datastucture;

import java.util.Stack;
/**
 * 实现拓扑排序
 * @author win7
 *
 */
public class TopologicalSort {
	/**
	 * 开始初始化入度为零的节点
	 * @param graph
	 * @param indegree
	 * @param n
	 */
	public static void findIndegree(int [][] graph,int [] indegree,int n){
		for(int i=0;i<n;i++){
			indegree[i]=0;
			for(int j=0;j<n;j++){
				if(graph[j][i]>0){
					indegree[i]++;
				}
			}
		}
	}
	/**
	 * 拓扑排序
	 * 算法思想:(1)在有向图中选择一个没有钱去的顶点且输出。(2)从图中删除该顶点和所有已他为尾的弧。
	 * 重复上面两部,直至所有的顶点均输出,或者当图中不存在无前驱的顶点为止,这时候说明图中有环。
	 * @param graph 矩阵表示的有向图
	 * @param n     有向图中节点个数
	 */
	public static void topologicalSort(int [] [] graph,int n){
		int [] indegree=new int[n];
		findIndegree(graph,indegree,n);
		Stack<Integer> s=new Stack<Integer>();
		for(int i=0;i<n;i++){
			if(indegree[i]==0){
				s.push(i);
			}	
		}
		while(!s.empty()){
			int start= s.pop();
			System.out.println("输出一个0入度节点:"+(start+1));
			for(int i=0;i<n;i++){
				if(graph[start][i]>0){
					indegree[i]--;
					if(indegree[i]==0){
						s.push(i);
					}
				}
			}
		}
		
	}
	
	public static void main(String [] args){
		int [][] graph={
				{-1,1,1,1,-1,-1},
				{-1,-1,-1,-1,-1,-1},
				{-1,1,-1,-1,1,-1},
				{-1,-1,-1,-1,1,-1},
				{-1,-1,-1,-1,-1,-1},
				{-1,-1,-1,1,1,-1}			
		};
		TopologicalSort.topologicalSort(graph, 6);
		
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++中的拓扑排序通常用于有向无环图(DAG, Directed Acyclic Graph)中,它将图中所有的节点按照它们的依赖关系排成线性的顺序。这种排序可以帮助我们确定任务的执行顺序,例如在一个任务图中,先完成的任务可以放在前面。 下面是一个简单的C++代码实现,使用邻接表来表示图,并使用深度优先搜索(DFS)进行排序: ```cpp #include <iostream> #include <vector> #include <list> using namespace std; // 图节点结构体 struct Node { int id; vector<int> adj; // 存储邻居节点 }; // 检查图是否强连通分量 bool isCyclic(Node* node, list<Node*> &visited, list<Node*>& result) { visited.push_back(node); bool hasCycle = false; for (int neighbor : node->adj) { if (visited.end() == find(visited.begin(), visited.end(), static_cast<Node*>(neighbor))) { hasCycle |= isCyclic(neighbor, visited, result); } } if (!hasCycle) { result.push_back(node); return false; } else { visited.pop_back(); return true; } } // 拓扑排序 void topologicalSort(vector<Node>& graph) { int n = graph.size(); list<Node*> nodes(n), result; // 初始化所有节点为未访问状态 for (int i = 0; i < n; ++i) nodes[i] = &graph[i]; // 对每个节点,如果发现没有从它指向的边,将其加入结果列表 for (Node* node : nodes) { if (isCyclic(node, list<Node*>(), result)) { // 如果存在环,则无法排序 cout << "Graph contains a cycle and is not a DAG." << endl; return; } } // 将节点按顺序添加到结果列表 while (!result.empty()) { Node* current = result.front(); result.pop_front(); cout << current->id << " "; for (int neighbor : current->adj) nodes.erase(find(nodes.begin(), nodes.end(), static_cast<Node*>(neighbor))); } } // 示例图构造 void buildExampleGraph(vector<Node>& graph) { Node node1{1, {2}}; Node node2{2, {3}}; Node node3{3, {}}; graph.push_back(node1); graph.push_back(node2); graph.push_back(node3); } int main() { vector<Node> graph; buildExampleGraph(graph); // 假设这里构造了一个示例图 cout << "Topological sort of the example graph: "; topologicalSort(graph); cout << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WitsMakeMen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值